/**
 * Created by dale on 2017/10/28.
 */
$(document).ready(function () {
    var form = new xhForm($('#xh_form'));
    form.parse().bindRequired($('#submit'));
    $(setTimeout(function () {
        var um = UM.getEditor('myEditor',{
            imageUrl:baseUrl+"frontend/article/uploadImgs", //处理图片上传的接口
            imageFieldName:"upfile", //上传图片的表单的name
            toolbar:['source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat |',
                'insertorderedlist insertunorderedlist | selectall cleardoc paragraph | fontfamily fontsize' ,
                '| justifyleft justifycenter justifyright justifyjustify |',
                'link unlink | image accessory',
                '| horizontal  formula'],
            imagePath : '',
        });

        $(".form-date").datetimepicker({format: 'yyyy-mm-dd', language: 'zh-CN', minView: "month", autoclose: true});
        var iArtType = addressUri.getParam('iArtType');
        var schoolId = addressUri.getParam('schoolId');
        var userId   = addressUri.getParam('userId');
        if(iArtType==3){
            $('#tTime').remove();
        }
        if(iArtType == 7){
            $('#xh_form').find('tr:first td').append('<input type="hidden" name="iSchoolId" value="'+schoolId+'"/>');

            if(userId != 'null'){
                $('#xh_form').find('tr:first td').append('<input type="hidden" name="iUserId" value="'+userId+'"/>');
            }else {
                $.ajax({
                    url:baseUrl+'article/getTch',
                    data:{schoolId:schoolId},
                    method:'post',
                    dataType:'json',
                    success:function (data) {
                        var str = '<tr><td style="width: 75px;"><label >教师</label></td><td style="width: 400px;"><select  name="iUserId" class="form-control"><option value="">请选择</option>';
                        $.each(data,function (id,text) {
                            str += '<option value="'+id+'">'+text+'</option>';
                        });
                        str += '</select></td>';
                        str += '<td  style="width: 75px;"><label>时间</label></td><td><input class="form-datetime form-control" name="tStartTime" type="text" ></td>';
                        str +=    '</tr>';
                        $('#xh_form').find('tr:first').after(str);
                        $('[name=iUserId]').select2();
                        $(".form-datetime").datetimepicker({format: 'yyyy-mm-dd 00:00:00', language: 'zh-CN', minView: 'month', autoclose: true});
                        if(artInfo!=1){
                            $('[name=iUserId]').val(artInfo['iUserId']).trigger('change');
                            $('[name=tStartTime]').val(artInfo['tStartTime']);
                        }
                    }
                });
            }
        }
        if(iArtType == 8){  //百分网使用
            $('#xh_form').find('tr:first').after('<tr>' +
                '                    <td style="width: 75px"><label>标签</label></td>' +
                '                    <td class="has-warning"><input name="sTag" must="true" type="text" class="form-control" >' +
                '                    </td>' +
                '                </tr>');
            form.bindRequired($('#submit'));
        }

        $('[name=iArtType]').val(iArtType);
        if(artInfo!=1){
            form.fill(artInfo);
            um.ready(function() {//编辑器初始化完成再赋值
                if(artInfo['mText']!=null){
                    um.setContent(artInfo['mText']);
                }
            });
        }
    },100));
    



    $('#submit').click(function () {
        form.submit('article/saveMsg',{},function (res) {
            $('#bt_modal_confirm',parent.document).addClass('disabled');
            window.top.xhTop.showSuccess(res.msg);
            window.top.xhTop.closeModal();
            if( $('[name=iArtType]').val() != 7){
                window.top.xhTop.getActiveFrame().location.reload();
            }else {
                window.top.xhTop.getActiveFrame()['caseFrame'].location.reload();
            }
        });
        // $.ajax({
        //     url:baseUrl+'frontend/article/saveMsg',
        //     data:$('#xh_form').serialize(),
        //     type:'POST',
        //     dataType:'json',
        //     success:function (data) {
        //         $('#bt_modal_confirm',parent.document).addClass('disabled');
        //         window.top.xhTop.closeModal();
        //         if( $('[name=iArtType]').val() != 7){
        //             window.top.xhTop.getActiveFrame().location.reload();
        //         }else {
        //             window.top.xhTop.getActiveFrame()['caseFrame'].location.reload();
        //         }
        //     },
        //     error:function (data) {
        //         console.log(data);
        //         window.top.swal(
        //             data['responseText']['error'],
        //             '',
        //             'error'
        //         );
        //     }
        // });
    });

    $('#cancel').click(function () {
        window.top.xhTop.closeModal();
    });

});
